package poker;

import java.util.ArrayList;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Map;

import enums.ActionType;
import enums.State;

public abstract class Player 
{
	private Card[] pocketCards;
	private int cash;
	private int postedCash;
	private boolean isAI;

	private String name;
	public int bettingRoundCounter;
	
	private int totalWinsThisGame;
	
	private boolean folded;
	private Action lastAction;
	
	private Map<Action, State> roundActions;
	
	public Player(int startCash, String name)
	{
		pocketCards = new Card[2];
		cash = startCash;
		this.name = name;
		this.folded = false;
		bettingRoundCounter = 2;
		postedCash = 0;
		isAI = true;
		roundActions = new HashMap<Action, State>();
		totalWinsThisGame = 0;
	}
	
	public String getName() { return name; }
	public int getCash() { return cash; }
	public Card[] getPocketCards(){ return pocketCards; }
	public boolean getFolded() { return folded; }
	public void setFolded(boolean folded) { this.folded = folded; } 
	public int getBettingRoundCounter() { return bettingRoundCounter; }
	public int getPostedCash() { return postedCash; }
	public void setPostedCash(int postedCash) {	this.postedCash = postedCash; }
	public Action getLastAction() { return lastAction; }
	public boolean getisAI(){ return isAI;}
	public void setIsAI(boolean value){ this.isAI = value;}

	public int getTotalWinsThisGame(){ return totalWinsThisGame;}
	public void setTotalWinsThisGame(int newWin){ this.totalWinsThisGame += newWin;}
	
	public void resetCards() 
	{
		pocketCards[0] = null;
		pocketCards[1] = null;
	}
	
	public void addCash(int sum) {
		cash += sum;
	}
	
	public String getPocketCardsAsString() 
	{
		Card card1 = pocketCards[0];
		Card card2 = pocketCards[1];
		
		String pocketCards = card1.toString() + " " + card2.toString();
		
		return pocketCards;
	}
	
	public void setPocketCards(Card pocketCard) 
	{
		if(pocketCards[0] == null) 
			this.pocketCards[0] = pocketCard;
		else
			this.pocketCards[1] = pocketCard;
	}	
	
	public abstract ActionType chooseAction(GameRound gameRound);
	
	public Action doAction(GameRound gameRound) 
	{
		
		ActionType chosenActionType = chooseAction(gameRound);
		Action action = null;
		State state = gameRound.getState();
		
		if(chosenActionType == ActionType.FOLD)
		{
			action = Action.Fold(state);
			this.setFolded(true);
			Printer.printActionFold(this);
		}
		if(chosenActionType == ActionType.CHECK)
		{
			action = Action.Check(state);
			Printer.printActionCheck(this);
		}
		if(chosenActionType == ActionType.CALL)
		{ 
			int callSum = gameRound.getCurrentBet() - getPostedCash();
			if(callSum == 0)
			{
				action = Action.Check(state);
				Printer.printActionCheck(this);
			}
			else
			{
				action = Action.Call(callSum, state);
				Printer.printActionCall(this, callSum + getPostedCash());				
			}
		}
		if(chosenActionType == ActionType.RAISE)
		{
			int raiseSum = gameRound.getCurrentBet() + 100 - getPostedCash();
			Printer.printActionRaise(this, raiseSum + getPostedCash());

			action = Action.Raise(raiseSum, state);
		}
		if(chosenActionType == ActionType.BET)
		{
			int betSum = 100;
			Printer.printActionBet(this, betSum);
			action = Action.Bet(betSum, state);
		}
		this.lastAction = action;

		return action;
	}
	
	public double getPotOdds(GameRound gameRound)
	{
		 int callSum = gameRound.getCurrentBet() - getPostedCash();
		 return callSum / (callSum + gameRound.getPot());
	}
	
	@Override
	public String toString() {
		return this.getName();
	}
}